//
//  AlertWindow.m
//  UIWindow
//
//  Created by 韩潇雨 on 2017/3/23.
//  Copyright © 2017年 sinbahan. All rights reserved.
//

#import "WSDataReportWindow.h"
#import "WSDataReportFloatingListViewController.h"
#import "WSDataReportConfig.h"

@interface WSDataReportWindow()<WSDataReportFloatingDelegate>

@property (nonatomic, strong) UIButton *dataReportShowButton;
@property (nonatomic, strong) WSDataReportFloatingListViewController *floatingListViewController;

@end


@implementation WSDataReportWindow


- (instancetype)initWithListViewShowKeyArray:(NSArray<NSString *> *)listViewShowKeyArray
             dataFilterItemArray:(NSArray<WSDataReportFilterItem *> *)dataFilterItemArray {
    self = [super initWithFrame:UIScreen.mainScreen.bounds];
    if (self) {
        [WSDataReportConfig instance].listViewShowKeyArray = listViewShowKeyArray;
        [WSDataReportConfig instance].dataFilterItemArray = dataFilterItemArray;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow makeKeyAndVisible];
    self.backgroundColor = UIColor.clearColor;
    self.windowLevel = UIWindowLevelAlert + 1;
    [self addSubview:self.dataReportShowButton];
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.dataReportShowButton addGestureRecognizer:panGesture];
    self.rootViewController = self.floatingListViewController;
}


#pragma mark - Rewrite System Method

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    UIView *view = [super hitTest:point withEvent:event];
    if (view == self) {
        return nil;
    }
    return view;
}

#pragma mark - WSDataReportFloatingDelegate Method

- (void)closeReportFloatingPageView {
    self.dataReportShowButton.hidden = NO;
    self.floatingListViewController.view.hidden = YES;
}

#pragma mark - GestureRecognizer Method

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint location = [recognizer locationInView:self];
        if (location.y < 0 || location.y > self.bounds.size.height) {
            return;
        }
        CGPoint translation = [recognizer translationInView:self];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,recognizer.view.center.y + translation.y);
        [recognizer setTranslation:CGPointZero inView:self];
    }
}

#pragma mark - Private Method

- (void)showDataReportFloatList {
    self.dataReportShowButton.hidden = YES;
    self.floatingListViewController.view.hidden = NO;
}

#pragma mark - Public Method

-(void)hideWithAnimation {
    [self hideWithAnimation:YES];
}

- (void)showWithAnimation:(BOOL)animation {
    [UIView animateWithDuration:animation ? 0.3 : 0
                     animations:^{
        self.hidden = NO;
    } completion:nil];
}

- (void)hideWithAnimation:(BOOL)animation {
    [UIView animateWithDuration:animation ? 0.3 : 0
                     animations:^{
        self.hidden = YES;
        
    } completion:nil];
}

- (void)setDataReportWindowLevel:(UIWindowLevel)windowLevel {
    self.windowLevel = windowLevel;
}


#pragma mark - Accessor Method

- (WSDataReportFloatingListViewController *)floatingListViewController {
    if (!_floatingListViewController) {
        CGFloat kScreenWidth = UIScreen.mainScreen.bounds.size.width;
        CGFloat kScreenHeight = UIScreen.mainScreen.bounds.size.height;
        _floatingListViewController = [[WSDataReportFloatingListViewController alloc] init];
        _floatingListViewController.view.frame = CGRectMake(10, (kScreenHeight - 250) / 2, kScreenWidth - 20, 300);
        _floatingListViewController.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
        _floatingListViewController.delegate = self;
        _floatingListViewController.view.hidden = YES;
    }
    return _floatingListViewController;
}

- (UIButton *)dataReportShowButton {
    if (!_dataReportShowButton) {
        _dataReportShowButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
        _dataReportShowButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
        _dataReportShowButton.layer.cornerRadius = 20.f;
        _dataReportShowButton.layer.masksToBounds = YES;
        _dataReportShowButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _dataReportShowButton.hidden = YES;
        [_dataReportShowButton setTitle:@"展开新数据上报信息" forState:UIControlStateNormal];
        [_dataReportShowButton addTarget:self action:@selector(showDataReportFloatList) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dataReportShowButton;
}

#pragma mark - dealloc

- (void)dealloc {
    [self resignKeyWindow];
}



@end
